package com.fastloan.jn.mgt.process.controller;

import com.fastloan.jn.mgt.process.model.JnAppApproveFile;
import com.fastloan.jn.mgt.process.service.AppApproveFileService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;


/**
 * 审核文件
 * Created by chenkai on 2017/4/17.
 */
@RestController
@RequestMapping("/api/web/appApproveFile")
public class AppApproveFileController extends BaseController<JnAppApproveFile> {
    @Autowired
    private AppApproveFileService appApproveFileService;

    /**
     * 上传审核文件
     *
     * @param appApproveUserId 审核人id
     * @param files            文件
     * @return 成功/失败
     */
    @PostMapping("/upload")
    public ResultTo upload(@RequestParam("appApproveUserId") String appApproveUserId, MultipartFile[] files
            , @RequestParam("applicationId") String applicationId) {
        int upload = appApproveFileService.upload(files, appApproveUserId, applicationId);
        return upload != 0 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    /**
     * 查询已上传的文件
     *
     * @param appApproveUserId 审核人id
     * @param applicationId    进件id
     * @return 审核VO类
     */
    @RequestMapping("/{applicationId}")
    public ResultTo queryFile(@RequestParam(value = "appApproveUserId", defaultValue = "0") String appApproveUserId
            , @PathVariable("applicationId") String applicationId) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("applicationId", applicationId);
        map.put("appApproveUserId", appApproveUserId);
        return dataPage("queryFile", map);
    }


    /**
     * 刪除审核文件
     *
     * @param fileId 文件ID
     * @return 数据库变记录
     */
    @DeleteMapping("/{fileId}")
    public ResultTo deleteFile(@PathVariable("fileId") String fileId) {
        int flag = appApproveFileService.deleteFile(fileId);
        return flag != 0 ? new ResultTo().setData(flag) : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

}
